//node里面有什么东西呢？
// {
//     Val:Element 
//     next:Node
// }

class Node{
    //每一个node节点都是这个样子的
    constructor(element){
        this.val  = element
        this.next = null
    }
}

class NodeList{
    constructor(){
        this.size = 0
        this.head = null
    }
    //链表中有增删改查的操作

    //向链表中添加一个元素
    append(element){//在链表上添加元素
        if(this.head === null){
            this.head = new Node(element)
        }else{
            let current  = this.getNode(this.size - 1)
            current.next = new Node(element)
        }
        this.size++
    }

    getNode(size){
        if(size<0 || size>=this.size){
            throw new Error('越界')
        }else{
            let current = this.head
            for(let i = 0;i<index;i++){
                current = current.next
            }
            return current
        }
    }

    appendAt(){
        //在某个位置添加元素
    }

    removeAt(position){
        //在某个位置移除元素
        if(position<0 || position >this.size-1){
            throw new Error("position out range")
        }
        //删除的是头节点
        if(position === 0 ){
            let cur = this.head.next
            this.head = cur
        }else{
            let prev = this.getNode(position - 1)
            let current = this.getNode(position)
            prev.next = current.next
            //就相当于prev.next.next
        }
        this.size--
    }
}



